package com.kimi.weibo.service;

import java.util.LinkedList;
import java.util.Queue;

import android.app.Activity;
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;

import com.kimi.weibo.bean.Task;
import com.kimi.weibo.ui.base.IWeiboActivity;

/**
 * 
 * 项目名称：Myweibo 类名称：MainService 类描述：Myweibo核心服务，线程监听任务 并进行处理 创建人：kimi
 * 创建时间：2013-1-15 上午9:44:47 修改人：kimi 修改时间：2013-1-15 上午9:44:47 修改备注：
 * 
 * @version
 * 
 */
public class MainService extends Service implements Runnable {

	private static final Queue<Task> tasks = new LinkedList<Task>();
	private static final LinkedList<Activity> appActivities = new LinkedList<Activity>();
	private boolean isRun;

	/**
	 * 将Task压栈
	 * 
	 * @param task
	 */
	public static void putToTasks(Task task) {
		tasks.add(task);
	}

	/**
	 * 将需要更新的UI界面压栈
	 * 
	 * @param activity
	 */
	public static void putToActivitis(Activity activity) {
		appActivities.add(activity);
	}

	
	/**
	 * 
	 * 复写onCreate()方法，启动线程监听任务队列
	 * @return
	 */
	@Override
	public void onCreate() {

		isRun = true;
		Thread thread = new Thread(this);
		thread.start();
		super.onCreate();
	}

	/**
	 * Handler处理消息队列
	 */
	Handler handler = new Handler() {
		/*
		 * (non-Javadoc)
		 * 
		 * @see android.os.Handler#handleMessage(android.os.Message)
		 */
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			switch (msg.what) {
			case Task.USER_LOGIN:// 用户登陆
				// 更新UI
				IWeiboActivity activity = (IWeiboActivity) getActivityByName("LoginActivity");
				activity.refresh(msg.obj);
				break;

			default:
				break;
			}
		}
	};

	@Override
	public void run() {
		// TODO Auto-generated method stub
		while (isRun) {
			Task task = null;
			if (!tasks.isEmpty()) {
				task = tasks.poll();// 执行完任务后把改任务从任务队列中移除
				if (null != task) {
					this.doTask(task);
				}
			}
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

	/**
	 * 根据Activity 的Name 获取Activity对象吧
	 * 
	 * @param name
	 * @return Activity
	 */
	protected Activity getActivityByName(String name) {
		// TODO Auto-generated method stub
		if (!appActivities.isEmpty()) {
			for (Activity activity : appActivities) {
				if (activity != null) {
					if (activity.getClass().getName().indexOf(name) > 0) {
						return activity;
					}
				}
			}
		}
		return null;
	}

	/**
	 * 任务处理方法
	 * 
	 * @param task
	 */
	private void doTask(Task task) {
		// TODO Auto-generated method stub
		Message msg = handler.obtainMessage();
		msg.what = task.getTaskId();
		switch (task.getTaskId()) {
		case Task.USER_LOGIN:
			System.out.println("doTask >>>>>  user Login");
			msg.obj = "登陆成功";
			break;

		default:
			break;
		}
		handler.sendMessage(msg);
	}

	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}

}
